package com.gz.steak.modules.system.controller;

import com.gz.steak.common.aspect.anotation.Auth;
import com.gz.steak.common.aspect.anotation.OperationLog;
import com.gz.steak.common.web.base.BaseController;
import com.gz.steak.common.consts.Role;
import com.gz.steak.modules.system.pojo.dataobject.SysUser;
import com.gz.steak.modules.system.pojo.vo.SysUserEditVo;
import com.gz.steak.modules.system.pojo.vo.SysUserVo;
import com.gz.steak.modules.system.service.SysUserService;
import com.gz.steak.utils.jwt.TokenUtil;
import com.gz.steak.common.web.result.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author 刘牌
 * @Date 2020/3/20 0020 19:51
 * @Version 1.0
 */

@RestController
@RequestMapping("/user")
public class SysUserController extends BaseController {

    private final static Logger logger = LoggerFactory.getLogger(SysUserController.class);

    @Autowired
    private SysUserService sysUserService;

    /**
     * 添加用户
     * @param sysUser
     * @param request
     * @return
     */
    @OperationLog("添加用户")
    @Auth(Role.ADMIN)
    @PostMapping("/save")
    public Result save(@RequestBody SysUser sysUser , HttpServletRequest request){
        logger.info("user "+sysUser);
        sysUserService.save(sysUser,TokenUtil.getUser(request));
        return Result.success("success");
    }

    /**
     * 用户列表
     * @return
     * params:@RequestParam int nowPage , @RequestParam int pageSize
     */
    @GetMapping("/list")
    public Result list(@RequestParam Long nodeId){
        List<SysUserVo> list = sysUserService.list(nodeId);
        return Result.success(list);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById")
    public Result deleteById(@RequestParam Long id){
        sysUserService.deleteById(id);
        return Result.success("success");
    }

    /**
     * 冻结用户
     * @param id
     * @return
     */
    @Auth(Role.ADMIN)
    @PutMapping("/frozenById")
    public Result frozenById(@RequestParam Long id , @RequestParam int status){
        sysUserService.frozenById(id,status);
        return Result.success("success");
    }

    /**
     * 修改用户
     * @param sysUserEditVo
     * @return
     */
    @Auth(Role.ADMIN)
    @PutMapping("/edit")
    public Result edit(@RequestBody SysUserEditVo sysUserEditVo){
        logger.info("user  "+sysUserEditVo);
        sysUserService.edit(sysUserEditVo);
        return Result.success("success");
    }

}
